<template>
  <div class="all-video-box" >
     <TipsView :icon="Tipsicon" :text="Tipstext" ref="childTips"></TipsView>
              <div>
                <div class="video-img" @click.stop="gotoTvInfo(data.video_id)">
                  <img class="video-img-cover" :src="data.video_cover.video_cover_url" :style="coverSize" alt="" />
                  <div class="duration">{{duration}}</div>
                </div>
                <div class="video-bottom">
                  <div class="video-infotitle">{{data.video_title}}</div>
                <div class="publish-author">
                  <div class="left" v-if="avatarState">
                    <div class="publish-author-img" @click.stop="goUserHome(data.uid)">
                      <img :src="data.avatar" :alt="data.username" />
                    </div>
                  </div>
                  <div class="right">
                    <div class="publish-author-name" @click.stop="goUserHome(data.uid)">@{{data.username}}</div>
                    <div class="publish-info">
                      <span class="views">{{data.video_play_num}}次观看 </span> •
                      <span class="publish-days">{{data.video_time}}</span>
                    </div>
                  </div>
                </div>
                </div>

              </div>
            </div>
</template>

<script>
import http from '@/util/http'
import TipsView from '@/components/utilView/tipView.vue'
import { mapState } from 'vuex'
export default {
  components: {
    TipsView
  },
  props: {
    data: {
      type: Object,
      default: null
    },
    avatarState: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      Tipsicon: 'icon-jinggao',
      Tipstext: ''
    }
  },

  computed: {
    ...mapState(['user']),
    // 显示视频封面的中心部分
    coverSize () {
      const a = {}
      if (this.data.video_cover.width >= this.data.video_cover.height) {
        a.width = this.data.video_cover.width / this.data.video_cover.height * 100 + '%'
        a.left = -(this.data.video_cover.width / this.data.video_cover.height - 1) / 2 * 100 + '%'
        a.height = '100%'
      } else {
        a.height = this.data.video_cover.height / this.data.video_cover.width * 100 + '%'
        a.top = -(this.data.video_cover.height / this.data.video_cover.width - 1) / 2 * 100 + '%'
        a.width = '100%'
      }
      return a
    },
    // 显示视频的时长
    duration () {
      let time = ''
      if (this.data.video_duration < 60) {
        let miao = this.data.video_duration
        if (this.data.video_duration < 10) {
          miao = '0' + this.data.video_duration
        }

        time = '0:' + miao
      } else if (this.data.video_duration > 60 && this.data.video_duration < 3600) {
        let fen = null
        let miao = null
        fen = Math.trunc(this.data.video_duration / 60)
        miao = Math.trunc(this.data.video_duration % 60)
        if (miao < 10) {
          miao = '0' + miao
        }
        if (fen < 10) {
          fen = '0' + fen
        }
        time = fen + ':' + miao
      } else if (this.data.video_duration > 3600) {
        let shi = null
        let fen = null
        let miao = null
        shi = Math.trunc(this.data.video_duration / 3600)
        fen = Math.trunc((this.data.video_duration % 36000) / 60)
        miao = Math.trunc(this.data.video_duration % 60)
        if (miao < 10) {
          miao = '0' + miao
        }
        if (fen < 10) {
          fen = '0' + fen
        }
        time = shi + ':' + fen + ':' + miao
      }

      return time
    }
  },
  methods: {
    tipsMethod (text) {
      this.$refs.childTips.HanderTiming(1200)
      this.Tipstext = text
    },
    // 跳转到查看其他用户详情
    goUserHome (uuid) {
      // 校验登录
      const checkState = this.$checkU()
      if (!checkState) {
        this.tipsMethod('请先登录')
        return
      }
      if (this.user.uid === uuid) {
        this.$router.push({ path: '/user/myhome' })
      } else {
        this.$router.push({ path: '/home/userHome', query: { uuid: uuid } })
      }
    },
    // 跳转到视频详情页面
    gotoTvInfo (vid) {
      this.$router.push({ path: '/videodetail', query: { videoId: vid } })
      // 记录播放历史
      // 传递观看者的id和视频的id
      http.addTvhistory(this.user.uid, vid).then(res => {
        console.log(res.data)
      })
    }
    //

  }
}
</script>

<style lang='scss' scoped>
 .all-video-box {
            width: 32%;
            background: #fff;
            border-radius: 5px;
            cursor: pointer;
            margin: 25px 0 0 8px;

            .video-img {
              width: 100%;
              height: 180px;
              overflow: hidden;
              border-radius: 5px 5px 0 0;
              position: relative;
              .video-img-cover{
                // position: absolute;
                // display: block;
                width: 100%;
                height: 100%;
                border-radius: 5px 5px 0 0;
                transition: all 0.3s;
                object-fit: cover;

                &:hover {
                  filter: brightness(70%);
                  transform: scale(1.1);
                  opacity: 1;
                }
              }

              .duration {
                color: #eee;
                font-size: 12px;
                padding: 0 5px;
                background: rgba($color: #000, $alpha: 0.8);
                position: absolute;
                bottom: 5%;
                right: 5%;
              }
            }
            .video-infotitle {
              font-size: 14px;
              font-weight: 700;
              color: #333;
              padding: 10px 5px  5px;
              height: 40px;
              line-height: 20px;
                display: -webkit-box;
                -webkit-line-clamp: 2; //显示两行he
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis; //多的用省略号显示
                word-wrap: break-word; //在合适的点换行
                word-break: break-all; //允许长单词换行到下一行
                cursor: pointer;
                padding-bottom: 2px;
                overflow: hidden;

              &:hover {
                color: #f90;
              }
            }
            .publish-author {
              width: 100%;
              display: flex;
              align-items: center;
              padding-bottom: 10px;

              .left {
                width: 15%;
                padding: 0 15px;

                .publish-author-img {
                  width: 36px;
                  height: 36px;
                  border-radius: 50%;
                  overflow: hidden;
                  border: 1px solid #f5f5f5;

                  img {
                    display: block;
                    width: 100%;
                    border-radius: 50%;

                    &:hover {
                      filter: brightness(70%);
                      opacity: 1;
                    }
                  }
                }
              }
              .right {
                width: 80%;
                line-height: 18px;
                font-size: 12px;
                color: #939393;

                .publish-author-name {
                  &:hover {
                    color: #f90;
                  }
                }
              }
            }
          }
// and (min-width: 540px)
@media screen and (min-width: 540px)  and (max-width:768px) {
    .all-video-box {
            width: 49%;
            background: #fff;
            border-radius: 5px;
            cursor: pointer;
            margin: 0.2rem 0 0 0.08rem;

            .video-img {
              width: 100%;
              height: 180px;
              overflow: hidden;
              border-radius: 5px 5px 0 0;
              position: relative;
              .video-img-cover{
                // position: absolute;
                // display: block;
                width: 100%;
                height: 100%;
                border-radius: 5px 5px 0 0;
                transition: all 0.3s;
                object-fit: cover;

                &:hover {
                  filter: brightness(70%);
                  transform: scale(1.1);
                  opacity: 1;
                }
              }

              .duration {
                color: #eee;
                font-size: .25rem;
                padding: 0 5px;
                background: rgba($color: #000, $alpha: 0.8);
                position: absolute;
                bottom: 5%;
                right: 5%;
              }
            }
            .video-infotitle {
              font-size: .3rem;
              font-weight: 700;
              color: #333;
              padding: 10px 5px  5px;
              height: 40px;
              line-height: 20px;
                display: -webkit-box;
                -webkit-line-clamp: 2; //显示两行he
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis; //多的用省略号显示
                word-wrap: break-word; //在合适的点换行
                word-break: break-all; //允许长单词换行到下一行
                cursor: pointer;
                padding-bottom: 2px;
                overflow: hidden;

              &:hover {
                color: #f90;
              }
            }
            .publish-author {
              width: 100%;
              display: flex;
              justify-content: space-between;
              align-items: center;
              padding-bottom: 10px;

              .left {
                width: 15%;
                padding: 0 .15rem;

                .publish-author-img {
                  width: 32px;
                  height: 32px;
                  border-radius: 50%;
                  overflow: hidden;
                  border: 1px solid #f5f5f5;

                  img {
                    display: block;
                    width: 100%;
                    border-radius: 50%;

                    &:hover {
                      filter: brightness(70%);
                      opacity: 1;
                    }
                  }
                }
              }
              .right {
                width: 4.2rem;
                line-height: 18px;
                font-size: .25rem;
                color: #939393;

                .publish-author-name {
                  font-size: .25rem;
                  &:hover {
                    color: #f90;
                  }
                }
              }
            }
          }
}

@media screen   and (max-width:540px) {
    .all-video-box {
            width: 47%;
            background: #fff;
            border-radius: 5px;
            cursor: pointer;
            margin: 0.25rem 0 0 0.2rem;

            .video-img {
              width: 100%;
              height: 3rem;
              overflow: hidden;
              border-radius: 5px 5px 0 0;
              position: relative;
              .video-img-cover{
                // position: absolute;
                // display: block;
                width: 100%;
                height: 100%;
                border-radius: 5px 5px 0 0;
                transition: all 0.3s;
                object-fit: cover;

                &:hover {
                  filter: brightness(70%);
                  transform: scale(1.1);
                  opacity: 1;
                }
              }

              .duration {
                color: #eee;
                font-size: .25rem;
                padding: 0 5px;
                background: rgba($color: #000, $alpha: 0.8);
                position: absolute;
                bottom: 5%;
                right: 5%;
              }
            }
            .video-infotitle {
              font-size: .3rem;
              font-weight: 700;
              color: #333;
              padding: 10px 5px  5px;
              height: 40px;
              line-height: 20px;
                display: -webkit-box;
                -webkit-line-clamp: 2; //显示两行he
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis; //多的用省略号显示
                word-wrap: break-word; //在合适的点换行
                word-break: break-all; //允许长单词换行到下一行
                cursor: pointer;
                padding-bottom: 2px;
                overflow: hidden;

              &:hover {
                color: #f90;
              }
            }
            .publish-author {
              width: 100%;
              display: flex;
              justify-content: space-between;
              align-items: center;
              padding-bottom: 10px;

              .left {
                width: 15%;
                padding: 0 .27rem;

                .publish-author-img {
                  width: 32px;
                  height: 32px;
                  border-radius: 50%;
                  overflow: hidden;
                  border: 1px solid #f5f5f5;

                  img {
                    display: block;
                    width: 100%;
                    border-radius: 50%;

                    &:hover {
                      filter: brightness(70%);
                      opacity: 1;
                    }
                  }
                }
              }
              .right {
                width: 4.2rem;
                line-height: 18px;
                font-size: .05rem;
                color: #939393;

                .publish-author-name {
                  font-size: .05rem;
                  &:hover {
                    color: #f90;
                  }
                }
              }
            }
          }
}

</style>
